package beaver.game;

import java.io.Serializable;
import java.util.Random;


/**
 * This class will contain all information about a player
 * @author Davide Cannucci
 *
 */
public class Player implements Serializable{

	private static final long serialVersionUID = 1L;
	private String[] names = {"Raoul","Jesse","Steve","Ralph","Scooter","Jenny","James"};
	private int id;
	private String name;
	private Beaver[] team;
	private int currentBeaver;
	
	public Player(String playerName){
		Random generator = new Random();
		id = generator.nextInt(10000000);
		name = playerName;
		team = new Beaver[Constants.BEAVERS_PER_TEAM];
		for(int i = 0;i<team.length;i++){
			team[i] = new Beaver(id+(i+1),names[(id+i+1)%names.length],id);
		}
		
		currentBeaver = (generator.nextInt(100)%4);
	}
	
	public String getName(){
		return name;
	}
	
	public void nextBeaver(){
		//Need to check if beaver is alive.  What do we do when all beavers are dead?
		currentBeaver = (currentBeaver + 1)%4;
		while(!hasLost() && !team[currentBeaver].isAlive()){
			currentBeaver = (currentBeaver + 1)%4;
		}
			
		
		
	}
	
	public Beaver getCurrentBeaver(){
		return team[currentBeaver];
	}
	
	public Beaver[] getTeam(){
		return team;
	}
	
	public int getID(){
		return id;
	}
	
	public boolean hasLost(){
		boolean allDead = true;
		for(int i =0;i<team.length;i++){
			if(team[i].isAlive()){
				allDead = false;
				break;
			}
		}
		return allDead;
	}
	
	public int getLiveBeavers(){
		int live = 0;
		for(int i = 0;i<team.length;i++){
			if(team[i].isAlive()){
				live++;
			}
		}
		return live;
	}
	
	public int getDeadBeavers(){
		return (4 - getLiveBeavers());
	}
	
	public int getTotalHP(){
		int aggregateHP = 0;
		for(int i = 0;i<team.length;i++){
			aggregateHP = aggregateHP + team[i].getHP();
		}
		return aggregateHP;
	}
	
	public int getTotalKBs(){
		int aggregateKBs = 0;
		for(int i = 0;i<team.length;i++){
			aggregateKBs = aggregateKBs + team[i].getKBs();
		}
		return aggregateKBs;
	}
	
	public int getTotalDamage(){
		int aggregateDMG = 0;
		for(int i = 0;i<team.length;i++){
			aggregateDMG = aggregateDMG + team[i].getDamageDealt();
		}
		return aggregateDMG;
	}
	
	public boolean equals(Player a){
		return a.id == this.id;
	}
	
}